/** @format */

const app = getApp();
Page({
  /**
   * 页面的初始数据
   */
  data: {
    // 是否登录
    islogin: false,
    nickname: "", //名字
    // 头像
    headimg:
      "https://mmbiz.qpic.cn/mmbiz/icTdbqWNOwNRna42FI242Lcia07jQodd2FJGIYQfG0LAJGFxM4FbnQP6yfMxBgJ0F3YRqJCJ1aPAK2dQagdusBZg/0",
    theme: wx.getSystemInfoSync().theme,
  },
  // 登录方法
  onlogin() {
    console.log("我要直接登录");
    console.log(this.data.headimg, "头像");
    console.log(this.data.nickname);
    wx.setStorageSync("userInfo", {
      nickname: this.data.nickname,
      headimg: this.data.headimg,
      islogin: true,
    });
    this.setData({
      islogin: true,
    });
  },
  gotout() {
    console.log("退出登录");
    wx.setStorageSync("userInfo", {
      nickname: '',
      headimg: '',
      islogin: false,
    });
    this.setData({
      islogin: false,
      nickname: "用户名", //名字
      headimg:  "https://mmbiz.qpic.cn/mmbiz/icTdbqWNOwNRna42FI242Lcia07jQodd2FJGIYQfG0LAJGFxM4FbnQP6yfMxBgJ0F3YRqJCJ1aPAK2dQagdusBZg/0", // 头像
    });
    console.log(this.data.nickname, 'sssssssss');
    wx.exitMiniProgram({success: (res) => {}})
  },
  onChooseAvatar(e) {
    console.log(e.detail);
    this.setData({
      headimg: e.detail.avatarUrl,
    });
  },
  // 提交表单
  formSubmit(e) {
    console.log(e);
    this.setData({
      nickname: e.detail.value.nickname,
    });
    wx.switchTab({
      url: "/pages/home/index",
    });
    this.onlogin();
  },
  onLoad(options) {
    const that = this;
    wx.getStorage({
      key: "userInfo",
      success(res) {
        console.log(res.data);
        that.setData({
          islogin: res.data.islogin,
          nickname: res.data.nickname,
          headimg: res.data.headimg,
        });
      },
    });
    console.log(this.data.userInfo, "个人信息");
    wx.onThemeChange((result) => {
      this.setData({
        theme: result.theme,
      });
    });
  },
});
